package com.sright.bcard.common.service.share.utils;

import lombok.Data;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author peter
 * @description
 * @date 2020/6/4
 */
public class WeekUtils {

    @Data
    public final static class WeekData {
        private String day;
        private String format;
        private Date begin;
        private Date end;
    }

    public static List<WeekData> getLast7Days() {
        List<WeekData> days = new ArrayList<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat dateFormat2 = new SimpleDateFormat("MM.dd");
        SimpleDateFormat dateFormat3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date nowDate = new Date();
        for (int i = 0; i < 7; i++) {
            Calendar rightNow = Calendar.getInstance();
            rightNow.setTime(nowDate);
            rightNow.add(Calendar.DAY_OF_YEAR, -i);
            Date date = rightNow.getTime();
            String day = dateFormat.format(date);
            String format = dateFormat2.format(date);

            WeekData weekData =  new WeekData();
            weekData.setDay(day);
            weekData.setFormat(format);

            String begin = day + " 00:00:00";
            String end = day + " 23:59:59";
            try {
                Date beginDate = dateFormat3.parse(begin);
                Date endDate = dateFormat3.parse(end);
                weekData.setBegin(beginDate);
                weekData.setEnd(endDate);
            }
            catch (Exception exception) {

            }

            days.add(weekData);
        }

        return days;
    }
}
